package cn.com.homeyoda.aimate.aihealth.ui.my.family.info;

import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wzq.mvvmsmart.utils.GlideLoadUtils;
import com.wzq.mvvmsmart.utils.TimeUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.bean.VideoShareListBean;

public class MemberInfoAdapter extends BaseQuickAdapter<VideoShareListBean.RowsBean, BaseViewHolder> {

    public MemberInfoAdapter(int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, VideoShareListBean.RowsBean item) {
        helper.setText(R.id.tv_phone_number, item.getLink());
        long expiredTime = TimeUtils.string2Millis(formatIso8601Date(item.getEndTime()));
        helper.setGone(R.id.tv_expired, System.currentTimeMillis() > expiredTime);
        ImageView ivAvatar = helper.getView(R.id.iv_member_avatar);
        GlideLoadUtils.loadImg(ivAvatar, item.getAvatar(), R.mipmap.ic_member1);
        helper.addOnClickListener(R.id.btn_modify_permission);
    }

    public static String formatIso8601Date(String isoTime) {
        if (TextUtils.isEmpty(isoTime)) return "";

        // 替换末尾的时区格式 "+08:00" -> "+0800"
        String formattedTime = isoTime.replaceFirst(":(?=[0-9]{2}$)", "");

        SimpleDateFormat isoFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.getDefault());
        SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

        try {
            Date date = isoFormat.parse(formattedTime);
            return targetFormat.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return "";
        }
    }

    @Override
    public void setNewData(@Nullable List<VideoShareListBean.RowsBean> data) {
        super.setNewData(data);
    }
}
